package lt.mii.optimum.components;

import java.util.HashSet;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

public abstract class Person {

	protected Set<Lesson> $lessons;

	protected String $name;

	protected Long $id;

	protected Map<Subject, Integer> $hours;



	Person() {
		$hours = new Hashtable<Subject, Integer>();
		$lessons = new HashSet<Lesson>();

	}



	public boolean isBusy(Lesson lesson) {
		return lesson.getStudents().contains(this);

	}



	public boolean isBusy(Flow flow) {
		return flow.getPersonList().contains(this);
	}



	public void addHour(Subject subject) {
		$hours.put(subject, $hours.get(subject) + 1);
	}



	public void deductHour(Subject subject) {
		assert ($hours.get(subject) != 0);
		$hours.put(subject, $hours.get(subject) - 1);
	}



	public void addLesson(Lesson lesson) {
		$lessons.add(lesson);
		deductHour(lesson.getSubject());
	}



	public void removeLesson(Lesson lesson) {
		$lessons.remove(lesson);
		addHour(lesson.getSubject());

	}



	public boolean stillHasHours() {
		Iterator<Entry<Subject, Integer>> it = $hours.entrySet().iterator();
		while (it.hasNext()) {
			Entry<Subject, Integer> entry = it.next();
			if (entry.getValue() > 0)
				return true;
		}
		return false;
	}



	public Long getId() {
		return $id;
	}



	public void setId(Long id) {
		this.$id = id;
	}



	public void setLessons(Set<Lesson> lessons) {
		this.$lessons = lessons;
	}



	public Set<Lesson> getLessons() {
		return this.$lessons;
	}



	public void setName(String name) {
		this.$name = name;
	}



	public String getName() {
		return $name;
	}



	public Integer getHours(Subject subj) {
		return $hours.get(subj);
	}



	public void setHours(Subject subject, Integer value) {
		$hours.put(subject, value);
	}



	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		final Person other = (Person) obj;

		if ($name == null) {
			if (other.$name != null)
				return false;
		} else if (!$name.equals(other.$name))
			return false;
		return true;
	}



	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + (($id == null) ? 0 : $id.hashCode());
		result = prime * result + (($name == null) ? 0 : $name.hashCode());
		return result;
	}

}
